/**
 * Created on 18 nov. 2011 at 14:10:38 by HozakaN
 */
package fr.castorhoz.isi_connectivity_widget.widget_configuration;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import fr.castorhoz.isi_connectivity_widget.ConnButton;
import fr.castorhoz.isi_connectivity_widget.R;
import fr.castorhoz.isi_connectivity_widget.database.DBAdapter;
import fr.castorhoz.isi_connectivity_widget.widget_configuration.custom_adapters.WidgetListAdapter;

/**
 * @author jlimbert
 *
 */
public class WidgetConfiguration extends Activity implements Observer {
	
	public static final String ADD_WIDGET = "addWidget";
	public static final String REMOVE_WIDGET = "removeWidget";
	
	private List<ConnButton.widgets> widgets = new ArrayList<ConnButton.widgets>();
	private Set<String> selectedWidgets = new HashSet<String>();
	private WidgetListAdapter wla;
	private List<ImageView> images = new ArrayList<ImageView>();
	private DBAdapter db;
	
	private LinearLayout visualisationLayout;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.widget_configuration_layout);
		
        visualisationLayout = (LinearLayout) findViewById(R.id.visualisation_layout);
        visualisationLayout.setGravity(Gravity.CENTER); //don't fucking know why this param doesn't work in the xml layout file
        
        //Database configuration
        db = new DBAdapter(getApplicationContext());
        
        ((Button)findViewById(R.id.saveConfigButton)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				saveConfig();
				//closeActivity();
			}
		});
        
		findWidgets();
		/*
		 * Temporaire
		 */
		/*widgets.add("WifiWidget");
		widgets.add("mobileDataWidget");
		widgets.add("gpsWidget");
		widgets.add("roamingWidget");
		widgets.add("bluetoothWidget");*/
		for (ConnButton.widgets widget : ConnButton.widgets.values()) {
			widgets.add(widget);
		}
		/*
		 * Fin temporaire
		 */

		/*File f = File.listRoots()[0];
		String [] entries = System.getProperty("java.class.path")
		.split(System.getProperty("path.separator"));
		if (f.exists()) {
			String[] contents = f.list();
			for (String content : contents) {
				widgets.add(content);
			}
		}*/
		/*for (String entry : entries) {
			widgets.add(entry);
		}*/
		/*PackageManager pm = getApplicationContext().getPackageManager();
		pm.*/
		
		wla = new WidgetListAdapter(this, getApplicationContext(), widgets);
		ListView lv = (ListView)findViewById(R.id.widgetListView);
		lv.setAdapter(wla);
	}


	private void saveConfig() {
		db.open();
		for (String widgetName : selectedWidgets) {
			db.insertWidget(widgetName);
		}
		db.close();
	}

	private void closeActivity() {
		finish();
	}

	/*
	 * TODO quand je me souviendrais comment parcourir les classes d'un package >_<
	 */
	private void findWidgets() {
		List<Class<? extends ConnButton>> classes = readClassesFrom("fr.castorhoz.isi_connectivity_widget");

	}

	/*
	 * TODO quand je me souviendrais comment parcourir les classes d'un package >_<
	 */
	private List<Class<? extends ConnButton>> readClassesFrom(String string) {
		// TODO Auto-generated method stub
		return null;
	}

	
	public void update(Observable observable, Object data) {
		if (data instanceof String[]) {
			if (((String[])data)[0].equals(ADD_WIDGET)) {
				addWidget(((String[])data)[1]);
			} else {
				removeWidget(((String[])data)[1]);
			}
		}
	}

	private void removeWidget(String widgetName) {
		for (ImageView iv : images) {
			if (iv.getTag().equals(widgetName)) {
				visualisationLayout.removeView(iv);
				images.remove(iv);
				selectedWidgets.remove(widgetName);
				break;
			}
		}
	}

	private void addWidget(String widgetName) {
		ImageView iv = new ImageView(getApplicationContext());
		int drawableId = ConnButton.widgets.getAdaptedDrawableId(widgetName);
		iv.setImageResource(drawableId);
		iv.setTag(widgetName);
		/*LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		iv.setLayoutParams(lp);*/
		visualisationLayout.addView(iv);
		images.add(iv);
		selectedWidgets.add(widgetName);
	}

}
